function [] = restoring_divider_tb()
    n = 5;
    d = 9;
    b_f = 7;
    x_q = n/d-2^-7;
    x_r = 2^-7;
    N = [0 1 0 1];
    D = [1 0 0 1];
    X_q = [1 0 0 0 1 1 1];
    X_r = [0 0 0 0 0 0 0 1];
    [A_q,A_r] = restoring_divider(N,D);
    assert(A_q,A_r,X_q,X_r,n,d,x_q,x_r,N,D);

    n = 8;
    d = 12;
    b_f = 7;
    x_q = n/d-2^-6;
    x_r = 2^-5;
    N = [1 0 0 0];
    D = [1 1 0 0];
    X_q = [1 0 1 0 1 0 1];
    X_r = [0 0 0 0 0 1 0 0];
    [A_q,A_r] = restoring_divider(N,D);
    assert(A_q,A_r,X_q,X_r,n,d,x_q,x_r,N,D);

    n = 37;
    d = 89;
    b_f = 13;
    x_q = n/d-2^-9-2^-10-2^-11-2^-13-2^-14;
    x_r = 2^-9+2^-10+2^-11+2^-13+2^-14;
    N = [0 1 0 0 1 0 1];
    D = [1 0 1 1 0 0 1];
    X_q = [0 1 1 0 1 0 1 0 0 1 1 0 1];
    X_r = [0 0 0 0 0 0 0 0 1 1 1 0 1 1];
    [A_q,A_r] = restoring_divider(N,D);
    assert(A_q,A_r,X_q,X_r,n,d,x_q,x_r,N,D);
end

function [] = assert(A_q,A_r,X_q,X_r,n,d,x_q,x_r,N,D)
    q_ = bin_to_dec(A_q,0);
    r_ = bin_to_dec(A_r,1);
    if(q_*d+r_== n)
        fprintf('\tYes: %.2f/%.2f=%.6f, rest=%.7f => %s/%s=%s, rest=%s\n',n,d,x_q,x_r, mat2str(N),mat2str(D),mat2str(A_q),mat2str(A_r*1));
    else
        fprintf('\tNo : %.2f/%.2f=%.6f, rest=%.7f => %s/%s=%s, rest=%s,\n correct is: q:%.7f,r:%.7f ,q:%s,r:%s\n',n,d,x_q,x_r, mat2str(N),mat2str(D),mat2str(A_q),mat2str(A_r*1),x_q,x_r,mat2str(X_q),mat2str(X_r)); 
    end
end

function [t] = bin_to_dec(T,a)
    len_t = length(T);
    t = 0;
    for i = 1:len_t,
        t = t + T(i)*2^-(i-a);
    end 
end
